<?php

class UserController extends Controller {

    public function salemAction(){
        if (!$user_info = self::model('user')->checkLogin()) {
            Response::ajax(USER_NO_LOGIN, '用户未登录');
        }
        $info=self::model('dealer_userinfo')->userInfo($user_info['uid']);
        $salesman=self::model('dealer_salem')->salemName($info['salesman']);
        Response::ajax(200, 'success', ['name'=>$info['name'],'salesman'=>$salesman['user_name'],'mobile'=>$salesman['mobile'],'type'=>$info['type']]);

    }

    public function salemInfoAction(){
        $salemType=Request::getParams('salemType');
        if (!$user_info = self::model('user')->checkLogin()) {
            Response::ajax(USER_NO_LOGIN, '用户未登录');
        }
        $info=self::model('dealer_userinfo')->userInfo($user_info['uid']);
        if(empty($info)){
            $salesman="";
        }else{
            if($salemType==1){
                $salesman= $info['name'];//进货商名称
            }else{
                $salesman=$info['salesman'];
            }
        }
        Response::ajax(200, 'success', ['salesman'=>$salesman,'salemType'=>$salemType]);
    }

    public function editSalemAction(){
        $salemType=Request::getParams('salemType');
        $salesman=Request::getParams('salesman');
        if (!$user_info = self::model('user')->checkLogin()) {
            Response::ajax(USER_NO_LOGIN, '用户未登录');
        }
        if($salemType==1){
            if(empty($salesman)){
                Response::ajax(NORMAL_ERROR, '请输入店铺名称！');
            }
            $res=self::model('dealer_userinfo')->salemNameAdd($salesman,$user_info['uid']);
        }else{
            if(empty($salesman)){
                Response::ajax(NORMAL_ERROR, '请输入推荐人工号！');
            }
            $res=self::model('dealer_userinfo')->salemNumberAdd($salesman,$user_info['uid']);
        }
        if($res['code']!=200){
            Response::ajax(NORMAL_ERROR, $res['msg']);
        }else{
            Response::ajax(200, '修改成功！');
        }
    }

	public function infoAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}

		if (!$user_info = self::model('user')->findById($user_info['uid'])) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if ($user_info['status'] == 2) {
			Response::ajax(USER_FORBID, '您的账号被屏蔽,请联系管理员处理');
		}
		$address_info=self::model('user_address')->getOne('uid=?',$user_info['uid']);
		if(!empty($address_info)){
		    $user_info['address_id']=$address_info['id'];
        }else{
            $user_info['address_id']=0;
        }
		Response::ajax(200, 'success', self::model('user')->getUserInfo($user_info));
	}

	public function dataAction() {
		$uid = intval(Request::getParams('uid'));
		if (!$user_info = self::model('user')->findById($uid)) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if ($user_info['status'] == 2) {
			Response::ajax(USER_FORBID, '您的账号被屏蔽,请联系管理员处理');
		}
//        $user_info['ticket'] = Request::getParams('ticket');
		unset($user_info['password'], $user_info['salt']);
		Response::ajax(200, 'success', self::model('user')->getUserInfo($user_info));
	}

	public function configAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if (!$user_info = self::model('user')->findById($user_info['uid'])) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$config = [
			'house_size', 'province', 'province_id', 'city', 'city_id', 'budget',
			'community', 'company_id', 'decorate', 'layout', 'style', 'market_id',
			'company', 'dec_type',
		];
		$update_data = [];
		foreach ($config as $value) {
			if ($row = Request::getParams($value)) {
				$update_data[$value] = $row;
			}
		}
		if (empty($update_data)) {
			Response::ajax(NORMAL_ERROR, '数据传递有误，修改失败');
		}
		if (self::model('user')->updateById($user_info['uid'], $update_data)) {
			Response::ajax(200, '设置成功');
		}
		Response::ajax(NORMAL_ERROR, '设置失败，请稍后再试');
	}

	public function settingAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if (!$user_info = self::model('user')->findById($user_info['uid'])) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$update = '';
		$nickname = Request::getParams('nickname');
		if ($nickname) {
			$update['nickname'] = $nickname;
		}
		$sex = Request::getParams('sex');
		if ($sex) {
			$update['sex'] = $sex;
		}
		$avatar = Request::getParams('avatar');
		if ($avatar) {
			$update['avatar'] = $avatar;
		}
		$autograph = Request::getParams('autograph');
		if ($autograph) {
			$update['autograph'] = $autograph;
		}
		if (!$update) {
			Response::ajax(NORMAL_ERROR, '设置失败，参数错误');
		}
		if (self::model('user')->updateById($user_info['uid'], $update)) {
			if ($nickname) {
				$flag = self::model('user_apply')->getOne('uid=?', $user_info['uid']);
				if ($flag) {
					$userapply['realname'] = $nickname;
					self::model('user_apply')->update($userapply, 'id=?', $flag['id']);
				}
			}
			Response::ajax(200, '设置成功');
		}
		Response::ajax(NORMAL_ERROR, '设置失败，请稍后再试');
	}

	public function loginAction() {
		$mobile = Request::getParams('username');
		$password = Request::getParams('password');
		if (!$mobile || !$password) {
			Response::ajax(NORMAL_ERROR, '手机号或密码不能为空');
		}
		if (!$user_info = self::model('user')->getOne('mobile=?', $mobile)) {
			Response::ajax(NORMAL_ERROR, '手机号填写有误');
		}
		if ($user_info['password'] != md5(md5($password) . $user_info['salt'])) {
			Response::ajax(NORMAL_ERROR, '账号密码填写有误,登录失败');
		}
		if ($user_info['status'] == 2) {
			Response::ajax(USER_FORBID, '您的账号被屏蔽,请联系管理员处理');
		}
		Response::ajax(200, '登录成功', self::model('user')->getLoginInfo($user_info));
	}

	public function logoutAction() {
		self::model('user')->logout();
		Response::ajax(200, '退出成功');
	}

	public function registerAction() {
		$user_info = [];
		$user_info['mobile'] = Request::getParams('mobile');
		$code = Request::getParams('code');
		$user_info['password'] = Request::getParams('password');
		$result = self::model('user')->validata($user_info);
		if ($result !== true) {
			Response::ajax(NORMAL_ERROR, $result);
		}
		if (self::model('user')->getOne('mobile=?', array($user_info['mobile']))) {
			Response::ajax(NORMAL_ERROR, '抱歉,您填写的手机号已注册');
		}
		if (!self::model('sms')->verify($user_info['mobile'], $code, smsModel::REIGISTER)) {
			Response::ajax(NORMAL_ERROR, smsModel::$msg);
		}

		$user_info['nickname'] = uniqid('wxk');
		$user_info['add_time'] = date('Y-m-d H:i:s');
		$user_info['salt'] = Text::randString(5);
		$user_info['password'] = md5(md5($user_info['password']) . $user_info['salt']);
		if ($user_info['uid'] = self::model('user')->insert($user_info, true)) {
			unset($user_info['password'], $user_info['salt']);
			Response::ajax(200, '登录成功', self::model('user')->getLoginInfo($user_info));
		}
		Response::ajax(NORMAL_ERROR, '注册失败,请稍后再试');
	}

	public function bindNewAccountAction() {
		$verify_token = Request::getParams('verify_token');
		$user_info = [];
		$user_info['mobile'] = Request::getParams('mobile');
		$code = Request::getParams('code');
		$user_info['password'] = Request::getParams('password');
		$result = self::model('user')->validata($user_info);
		if ($result !== true) {
			Response::ajax(NORMAL_ERROR, $result);
		}
		if (!$verify_data = self::model('user')->decodeVerifyToken($verify_token)) {
			Response::ajax(NORMAL_ERROR, '验证信息失败,绑定失败');
		}
		if (!$user_oauth_info = self::model('user_oauth')->getOne('id=?', array($verify_data['id']))) {
			Response::ajax(NORMAL_ERROR, '验证信息失败,绑定失败');
		}
		if ($user_oauth_info['uid'] && $user_info = self::model('user')->findById($user_oauth_info['uid'])) {
			Response::ajax(200, '您的账号已绑定', self::model('user')->getLoginInfo($user_info));
		}

		if (self::model('user')->getOne('mobile=?', array($user_info['mobile']))) {
			Response::ajax(NORMAL_ERROR, '抱歉,您填写的手机号已注册');
		}
		if (!self::model('sms')->verify($user_info['mobile'], $code, smsModel::REIGISTER)) {
			Response::ajax(NORMAL_ERROR, smsModel::$msg);
		}
		$user_info['avatar'] = $user_oauth_info['avatar'];
		$user_info['nickname'] = $user_oauth_info['nickname'];
		$user_info['sex'] = $user_oauth_info['sex'];
		$user_info['salt'] = Text::randString(5);
		$user_info['password'] = md5(md5($user_info['password']) . $user_info['salt']);
		$user_info['add_time'] = date('Y-m-d H:i:s');
		if ($user_info['uid'] = self::model('user')->insert($user_info, true)) {
			if (self::model('user_oauth')->update(array('uid' => $user_info['uid']), 'id=?', array($verify_data['id']))) {
				Response::ajax(200, '登录成功', self::model('user')->getLoginInfo($user_info));
			}
		}
		Response::ajax(NORMAL_ERROR, '注册失败,请稍后再试');
	}

	public function bindAccountAction() {
		$verify_token = Request::getParams('verify_token');
		$mobile = Request::getParams('mobile');
		$code = Request::getParams('code');
		if (!$mobile || !Validation::isMobile($mobile)) {
			Response::ajax(NORMAL_ERROR, '手机号码填写有误');
		}
		if (!$verify_data = self::model('user')->decodeVerifyToken($verify_token)) {
			Response::ajax(NORMAL_ERROR, '验证信息失败,绑定失败');
		}
		if (!$user_oauth_info = self::model('user_oauth')->getOne('id=?', array($verify_data['id']))) {
			Response::ajax(NORMAL_ERROR, '验证信息失败,绑定失败');
		}
		if ($user_oauth_info['uid'] && $user_info = self::model('user')->findById($user_oauth_info['uid'])) {
			Response::ajax(200, '您的账号已绑定', self::model('user')->getLoginInfo($user_info));
		}
		if (!$user_info = self::model('user')->getOne('mobile=?', array($mobile))) {
			Response::ajax(NORMAL_ERROR, '抱歉,该手机未注册,绑定失败');
		}
		if (self::model('user_oauth')->getOne('uid=? and oauth_id=?', array($user_info['uid'], $user_oauth_info['oauth_id']))) {
			Response::ajax(NORMAL_ERROR, '抱歉,该手机已绑定该渠道账号,绑定失败');
		}
		if (!self::model('sms')->verify($mobile, $code, smsModel::BIND)) {
			Response::ajax(NORMAL_ERROR, smsModel::$msg);
		}
		if (self::model('user_oauth')->update(array('uid' => $user_info['uid']), 'id=?', array($verify_data['id']))) {
			Response::ajax(200, 'success', self::model('user')->getLoginInfo($user_info));
		}
		Response::ajax(NORMAL_ERROR, '绑定失败,请稍后再试');
	}

	public function bindEmailAction() {
		$email = Request::getParams('email');
		$code = Request::getParams('code');
		if (!$email || !Validation::isEmail($email)) {
			Response::ajax(NORMAL_ERROR, '邮箱账号填写有误');
		}
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if (self::model('user')->getOne('email=?', array($email))) {
			Response::ajax(NORMAL_ERROR, '抱歉,该邮箱已被绑定');
		}
		if (!self::model('email')->verify($email, $code, emailModel::BIND)) {
			Response::ajax(NORMAL_ERROR, emailModel::$msg);
		}
		$update = [
			'email' => $email,
		];
		if (self::model('user')->updateById($user_info['uid'], $update)) {
			Response::ajax(200, '绑定邮箱成功');
		}
		Response::ajax(NORMAL_ERROR, '邮箱绑定失败，请稍后再试');
	}

	public function findPasswordAction() {
		$data = [];
		$mobile = Request::getParams('mobile');
		$code = Request::getParams('code');
		$password = Request::getParams('password');
		$result = self::model('user')->validata(['mobile' => $mobile, 'password' => $password]);
		if ($result !== true) {
			Response::ajax(NORMAL_ERROR, $result);
		}
		if (!self::model('sms')->verify($mobile, $code, smsModel::FINDPWD)) {
			Response::ajax(NORMAL_ERROR, smsModel::$msg);
		}
		if (!$user_info = self::model('user')->getOne('mobile=?', array($mobile))) {
			Response::ajax(NORMAL_ERROR, '用户信息查询失败,修改失败');
		}
		$data['salt'] = Text::randString(5);
		$data['password'] = md5(md5($password) . $data['salt']);
		if (self::model('user')->updateById($user_info['uid'], $data)) {
			Response::ajax(200, '修改密码成功!');
		}
		Response::ajax(NORMAL_ERROR, '修改密码失败,请稍后再试');
	}

	public function mobileCodeLoginAction() {
		$mobile = Request::getParams('mobile');
		$code = Request::getParams('code');
		if (!$mobile || !Validation::isMobile($mobile)) {
			Response::ajax(NORMAL_ERROR, '手机号码填写有误');
		}
		if (!self::model('sms')->verify($mobile, $code, smsModel::LOGIN)) {
			Response::ajax(NORMAL_ERROR, smsModel::$msg);
		}
		if (!$user_info = self::model('user')->getOne('mobile=?', array($mobile))) {
			Response::ajax(NORMAL_ERROR, '用户信息查询失败,登录失败');
		}
		Response::ajax(200, '登录成功', self::model('user')->getLoginInfo($user_info));
	}

	public function getEmailCodeAction() {
		$email = Request::getParams('email');
		$type = Request::getParams('type');
		if (!$email || !$type || !Validation::isEmail($email)) {
			Response::ajax(NORMAL_ERROR, '参数填写有误,邮件发送失败');
		}
		if ($type == emailModel::BIND) {
			if (!$user_info = self::model('user')->checkLogin()) {
				Response::ajax(USER_NO_LOGIN, '用户未登录');
			}
			if (self::model('user')->getOne('email=?', $email)) {
				Response::ajax(NORMAL_ERROR, '抱歉,您填写的邮箱已被绑定');
			}
			if (self::model('email')->send($email, $type)) {
				Response::ajax(200, '发送邮件验证码成功');
			}
		}
		Response::ajax(NORMAL_ERROR, '发送失败,请稍后再试');
	}

	public function getMobileCodeAction() {
		$mobile = Request::get('mobile');
		$type = Request::get('type');
		if (!$mobile || !Validation::isMobile($mobile)) {
			Response::ajax(NORMAL_ERROR, '手机号码填写有误');
		}
		if (!$type) {
			Response::ajax(NORMAL_ERROR, '短信类型必填');
		}
		if ($type == smsModel::FINDPWD || $type == smsModel::LOGIN || $type == smsModel::BIND) {
			if (!self::model('user')->getOne('mobile=?', array($mobile))) {
				Response::ajax(NORMAL_ERROR, '抱歉,手机号码未注册');
			}
		} elseif ($type == smsModel::REIGISTER) {
			if (self::model('user')->getOne('mobile=?', array($mobile))) {
				Response::ajax(NORMAL_ERROR, '抱歉,手机号码已注册');
			}
		} elseif ($type == smsModel::CHANGEMOBILE) {
			if (!$user_info = self::model('user')->checkLogin()) {
				Response::ajax(USER_NO_LOGIN, '用户未登录');
			}
			if (self::model('user')->getOne('mobile=?', array($mobile))) {
				Response::ajax(NORMAL_ERROR, '抱歉,手机号码已注册,无法绑定');
			}
		}
		if (!self::model('sms')->send($mobile, $type)) {
			Response::ajax(NORMAL_ERROR, smsModel::$msg);
		}
		Response::ajax(200, '短信验证码发送成功');
	}

	public function changePasswordAction() {
		$old_password = Request::getParams('old_password');
		$new_password = Request::getParams('new_password');
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if (!Validation::isLength($new_password, 8, 15)) {
			Response::ajax(NORMAL_ERROR, '新密码长度在8~15位');
		}
		if (!$user_info = self::model('user')->findById($user_info['uid'])) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if ($user_info['password'] != md5(md5($old_password) . $user_info['salt'])) {
			Response::ajax(NORMAL_ERROR, '当前密码填写有误');
		}
		if (!$new_password || !Validation::isLength($data['password'], 8, 12)) {
			Response::ajax(NORMAL_ERROR, '密码长度在8~50位');
		}
		$data = [];
		$data['salt'] = Text::randString(5);
		$data['password'] = md5(md5($new_password) . $data['salt']);
		if (self::model('user')->updateById($user_info['uid'], $data)) {
			Response::ajax(200, '修改密码成功!');
		}
		Response::ajax(NORMAL_ERROR, '修改密码失败,请稍后再试');
	}

	public function changeMobileAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$mobile = Request::getParams('mobile');
		if (!$mobile || !Validation::isMobile($mobile)) {
			Response::ajax(NORMAL_ERROR, '手机号填写有误');
		}
		if (self::model('user')->getOne('mobile=?', array($mobile))) {
			Response::ajax(NORMAL_ERROR, '抱歉,手机号码已注册,无法绑定');
		}
		$code = Request::getParams('code');
		if (!self::model('sms')->verify($mobile, $code, smsModel::CHANGEMOBILE)) {
			Response::ajax(NORMAL_ERROR, smsModel::$msg);
		}
		$update = [
			'mobile' => $mobile,
		];
		if (self::model('user')->updateById($user_info['uid'], $update)) {
			Response::ajax(200, '修改手机号成功');
		}
		Response::ajax(NORMAL_ERROR, '手机号修改失败，请稍后再试');
	}

	public function ApplyAction() {

		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$type = Request::getParams('type');
		if (!$type) {
			Response::ajax(NORMAL_ERROR, '请选择申请类型');
		}
		//查询申请
		$mobile = Request::getParams('mobile');
		if (!$mobile || !Validation::isMobile($mobile)) {
			Response::ajax(NORMAL_ERROR, '手机号填写有误');
		}

		$realname = Request::getParams('realname');
		if (!$realname) {
			Response::ajax(NORMAL_ERROR, '请输入商家名称');
		}
		$province = Request::getParams('province');
		if (!$province) {
			Response::ajax(NORMAL_ERROR, '请选择省份');
		}
		$city = Request::getParams('city');
		if (!$city) {
			Response::ajax(NORMAL_ERROR, '请选择城市');
		}
		$area = Request::getParams('area');
		if (!$area) {
			Response::ajax(NORMAL_ERROR, '请选择县区');
		}
		$idcard = Request::getParams('idcard');
		if (!$idcard) {
			Response::ajax(NORMAL_ERROR, '请输入身份证号码');
		}
		$idface = Request::getParams('idface');
		if (!$idface) {
			Response::ajax(NORMAL_ERROR, '请上传身份证正面');
		}
		$idreverse = Request::getParams('idreverse');
		if (!$idreverse) {
			Response::ajax(NORMAL_ERROR, '请上传身份证反面');
		}

		$id = Request::getParams('id');
		//获取系统设置的加盟费
		$rule = self::model('point_rule')->getOne('id=?', 1);
		if ($type == 1) {
			$title = '店主';
			$price = $rule['shop_price'];
		} else {
			$title = '经销商';
			$price = $rule['dealer_price'];
		}
		//查询是否申请
		$flag = self::model('user_apply')->getOne(['uid=?', 'type=?'], [$user_info['uid'], $type]);
		if ($flag) {
			if ($flag['status'] != 2) {
				Response::ajax(NORMAL_ERROR, '您已经申请申请过' . $title . '，不能重复申请');
			}

		}

		$puid = Request::getParams('puid');
		if ($puid) {
			$flags = self::model('user_apply')->getOne('uid=?', $puid);
			if ($flags['type'] == 1) {
				$puid = 0;
			} elseif ($flags['type'] == 2 && $type == 2) {
				$puid = 0;
			}
		}
		$userapply['uid'] = $user_info['uid'];
		$userapply['mobile'] = $mobile;
		$userapply['type'] = $type ? $type : 1;
		$userapply['realname'] = $realname;
		$userapply['nickname'] = $user_oauth_info['nickname'];
		$userapply['province'] = $province;
		$userapply['city'] = $city;
		$userapply['area'] = $area;
		$userapply['idcard'] = $idcard;
		$userapply['idface'] = $idface;
		$userapply['idreverse'] = $idreverse;
		$userapply['price'] = $price;
		$userapply['puid'] = $puid ? $puid : 0;
		$userapply['status'] = 0;
		$userapply['addtime'] = time();
		if ($id) {
			if (self::model('user_apply')->update($userapply, 'id=?', $id)) {
				$content = "恭喜您，您提交申请的麻吉Go" . $title . "成功";
				user_notifyModel::addNotify($user_info['uid'], 5, 0, $user_info, $content);
				Response::ajax(200, $title . '重新申请成功');
			}
		} else {
			if (self::model('user_apply')->insert($userapply, true)) {
				Response::ajax(200, $title . '申请成功');
			}
		}

		Response::ajax(NORMAL_ERROR, $title . '申请成功，请稍后再试');
	}

	public function payAction($orderInfo) {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$apply = self::model('user_apply')->getOne('uid=? and ispay=?', [$user_info['uid'], 0]);
		if (!$apply) {
			Response::ajax(NORMAL_ERROR, '下单失败,付款信息不错在');
		}
		$oauthInfo = self::model('user_oauth')->getOne('uid=? and oauth_id=?', [$user_info['uid'], user_oauthModel::MINIWX]);
		if (!$oauthInfo || !$oauthInfo['openid']) {
			Response::ajax(NORMAL_ERROR, '下单失败,未绑定小程序账号');
		}
		$data['orderno'] = date('YmdHis') . $apply['type'] . mt_rand(100000, 999999);
		self::model('user_apply')->update($data, 'id=?', $apply['id']);

		$wechat = new \WeChat\Pay(Configure::get('weixin'));
		$options = [
			'body' => '微信小程序支付下单',
			'out_trade_no' => $data['orderno'],
			'total_fee' => intval($apply['price'] * 100),
			'openid' => $oauthInfo['openid'],
			'trade_type' => 'JSAPI',
			'notify_url' => Request::getServerName() . Router::getBaseUrl() . '/order_notify/wxapply',
			'spbill_create_ip' => Client::getClientIp(),
		];
		$result = $wechat->createOrder($options);
		if (isset($result['result_code']) && strtoupper($result['result_code']) == 'SUCCESS') {
			$data = [];
			$data['appId'] = Configure::get('weixin.appid');
			$data['timeStamp'] = strval(time());
			$data['nonceStr'] = Text::randString(10);
			$data['package'] = 'prepay_id=' . $result['prepay_id'];
			$data['signType'] = 'MD5';
			$data['paySign'] = $wechat->getPaySign($data);
			$data['orderId'] = intval($orderInfo['id']);
			$data['payType'] = $orderInfo['pay_type'];
			Response::ajax(200, 'success', $data);
		}
		Log::write('微信订单创建失败:' . json_encode($result, 320), 'WARN');
		Response::ajax(SYSTEM_ERROR, 'FAIL:' . isset($result['msg']) ? $result['msg'] : '订单创建失败');
	}

	public function incomeAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if (!$user = self::model('user')->getOne('uid=?', $user_info['uid'])) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		if ($user['balance'] <= 0) {
			Response::ajax(NORMAL_ERROR, '提现失败，可提现金额不足');
		}
		$apply = self::model('user_apply')->getOne('uid=?', $user_info['uid']);
		$userapply['uid'] = $user_info['uid'];
		$userapply['price'] = $user['balance'];
		$userapply['realname'] = $apply['realname'];
		$userapply['status'] = 0;
		$userapply['addtime'] = time();
		if (self::model('user_income')->insert($userapply, true)) {
			$data['total_balance'] = $user['total_balance'] - $user['balance'];
			$data['balance'] = 0;
			self::model('user')->update($data, 'uid=?', $user_info['uid']);
			Response::ajax(200, '提现申请成功');
		}
		Response::ajax(NORMAL_ERROR, '提现申请失败，请稍后再试');
	}


	public function balanceRecordAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		$uid = $user_info['uid'];
		///$uid = 20;
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 20));
		$where = $value = [];
		$where[] = 'uid=?';
		$value[] = $uid;
		Response::ajax(200, 'success', self::model('user_balance')->getList($where, $value, $page, $pageSize));
	}

	//获取access_token
	public function get_access_token() {
		$appid = Configure::get('weixin.appid');
		$secret = Configure::get('weixin.appsecret');
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
		return $data = $this->curl_get($url);
	}

	public function curl_get($url) {
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$data = curl_exec($curl);
		$err = curl_error($curl);
		curl_close($curl);
		return $data;
	}
	//获得二维码
	public function getqrcodeAction() {
		if (!$user_info = self::model('user')->checkLogin()) {
			Response::ajax(USER_NO_LOGIN, '用户未登录');
		}
		header('content-type:image/jpg');
		$uid = $user_info['uid'];
		$data = array();
		$data['path'] = '/pages/index/index?suid=' . $uid . '&share=on';
		$data['width'] = 150;
		$data['is_hyaline'] = false;
		$data = json_encode($data);
		$access = json_decode($this->get_access_token(), true);
		$access_token = $access['access_token'];
		$url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" . $access_token;
		$da = $this->get_http_array($url, $data);
		//这里强调显示二维码可以直接写该访问路径，同时也可以使用curl保存到本地，详细用法可以加群或者加我扣扣
	}
	public function get_http_array($url, $post_data) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   //没有这个会自动输出，不用print_r();也会在后面多个1
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$output = curl_exec($ch);
		curl_close($ch);
		$out = json_decode($output);
		return $out;
	}

	/**
	 *
	 * @DateTime:	2018-08-03 18:24:15
	 * @Author:  LG
	 */
	public function FunName() {
		//生成图片
		$imgDir = 'uploadImg/';
		$filename = "nissangcj" . $mobile . ".jpg"; ///要生成的图片名字

		$xmlstr = $GLOBALS[HTTP_RAW_POST_DATA];
		if (empty($xmlstr)) {
			$xmlstr = file_get_contents('php://input');
		}

		$jpg = $xmlstr; //得到post过来的二进制原始数据
		if (empty($jpg)) {
			echo 'nostream';
			exit();
		}

		$file = fopen("./" . $imgDir . $filename, "w"); //打开文件准备写入
		fwrite($file, $jpg); //写入
		fclose($file); //关闭

		$filePath = './' . $imgDir . $filename;

//图片是否存在
		if (!file_exists($filePath)) {
			echo 'createFail';
			exit();
		}

	}

	public function listAction() {
		$user_info = self::model('user')->checkLogin();
		if ($user_info) {
			$uid = $user_info['uid'];
		} else {
			$uid = 0;
		}
		$keywords = Security::removeXss(Request::getParams('keywords'));
		$page = intval(Request::getParams('page', 1));
		$pageSize = intval(Request::getParams('pageSize', 20));
		$where = $value = [];
		if ($keywords) {
			$where[] = 'nickname like ?';
			$value[] = '%' . $keywords . '%';
		}
		Response::ajax(200, 'success', self::model('user')->getList($where, $value, $uid, $page, $pageSize));
	}
}
